package com.shangze.test.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.shangze.test.app.Application;

import butterknife.ButterKnife;

/**
 * Created by yjm on 2017/10/14.
 */

public abstract class BaseActivity<P extends BasePresenter> extends BaseToolbarTitle implements BaseView {

    public P presenter;
    // 进程提示框
    private ProgressDialog pDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(provideContentViewId());
        ButterKnife.bind(this);
        Application.getInstance().addActivity(this);
        this.presenter = createPresenter();
        if (presenter != null) {
            presenter.attachView(this);
        }
        initView();
    }

    /**
     * 数据处理
     */
    public abstract void initView();
    protected abstract int provideContentViewId();

    public P createPresenter() {
        return null;
    }

    @Override
    public void startActivity(Class clazz) {
        startActivity(new Intent(getContext(), clazz));
    }

    @Override
    public void startActivityForResult(Class clazz, int requestCode) {
        startActivityForResult(new Intent(getContext(), clazz), requestCode);
    }

    @Override
    public void startActivityForResult(Class clazz, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getContext(), clazz);
        intent.putExtra("data", bundle);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void startActivity(Class clazz, Bundle bundle) {
        startActivity(new Intent(getContext(), clazz).putExtra("data", bundle));
    }


    /**
     * 进程提示框
     *
     * @param noticeString 显示的提示消息
     */
    public void showProgressDialog(String noticeString, boolean cancelable) {
        dialogDismiss();
        pDialog = new ProgressDialog(getContext());
        pDialog.setCancelable(cancelable);
        pDialog.setMessage(noticeString);
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.show();
        pDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                pDialog = null;
            }
        });
        pDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                pDialog = null;
            }
        });
    }

    /**
     * 让提示框框消失
     */
    public void dialogDismiss() {
        if (pDialog != null) {
            pDialog.dismiss();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Application.getInstance().finishActivity(this);
    }


    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    public boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     *
     * @param token
     */
    public void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public Context getContext() {
        return this;
    }


    @Override
    public void finishActivity() {
        this.finish();
    }

}
